db = require '../utils/connect'
async = require 'async'
request = require 'request'
Article = require '../models/article'
moment = require 'moment'
log = require '../utils/log'

module.exports = 
	"get /account/:id/index": (req, res)->
		local = {}
		async.series [
			(callback)->
				sql = "select * from accounts where id = ?"
				db.queryOne sql, [req.params.id],(err, result)->
					local.account = result
					callback err
			(callback)->
				sql = "select articles_approved.id, articles_approved.digest, articles_approved.title, articles_approved.datetime from accounts join articles_approved on accounts.id=? and accounts.username = articles_approved.username order by articles_approved.id desc"
				db.runSql sql, [req.params.id], (err,result)->
					result.forEach (item)->
						item.datetime = moment(item.datetime).format('YYYY-MM-DD hh:mm')
					local.articles = result
					callback err		
		],(err)->
			log.error err if err
			res.render 'account-index', local